package v1

import (
	"card/app/http/controllers/api"
	"card/app/logic"
	"card/app/requests"
	res "card/app/response"
	"card/pkg/response"
	"github.com/gin-gonic/gin"
)

type ProfileController struct {
	api.BaseApiController
}

func (p *ProfileController) Profile(ctx *gin.Context) {
	memberLogic := logic.NewMember(ctx)
	memberInfo := memberLogic.CurrentUser()
	if memberInfo.ID == 0 {
		response.Warn(ctx, "获取用户信息失败")
		return
	}
	response.Data(ctx, res.Member{
		Id:         memberInfo.ID,
		Tel:        memberInfo.Tel,
		Amount:     memberInfo.Amount,
		Points:     memberInfo.Points,
		Avatar:     memberInfo.Avatar,
		Level:      memberInfo.Level,
		Nickname:   memberInfo.Nickname,
		Email:      memberInfo.Email,
		IsAgent:    memberInfo.IsAgent,
		InviteCode: memberInfo.InviteCode,
	})
}

func (p *ProfileController) UpdateUserInfo(ctx *gin.Context) {
	memberLogic := logic.NewMember(ctx)
	params := new(requests.MemberUpdateRequest)
	if err := requests.Validate(ctx, params, requests.MemberUpdate); err != nil {
		response.Error(ctx, err)
		return
	}
	err := memberLogic.UpdateUserInfo(params)
	if err != nil {
		response.Error(ctx, err)
		return
	}
	response.Success(ctx)
}
